import pymysql


class MySQLTest:

    def __init__(self):
        self.username = "root"
        self.host = "192.168.1.4"
        self.password = "123456"
        self.dbs = "user_manage"
        self.connect()

    # 连接MySQL数据库
    def connect(self):
        self.conn = pymysql.connect(host=self.host, password=self.password,
                        user=self.username, db=self.dbs)
        self.cur = self.conn.cursor()
        return self.cur

    def close_mysql(self):
        self.cur.close()
        self.conn.close()

    def fetch_all(self):
        cur = self.connect()
        sql = "select * from user"
        cur.execute(sql)
        result = cur.fetchall()
        for i in result:
            print(str(i))
        self.close_mysql()

    def insert_mysql(self):
        pass

    def fetchone(self, sql):
        self.cur.execute(sql)
        res = self.cur.fetchone()
        return res

    def update(self, sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print("------ 修改成功 ------")
        except Exception as e:
            print(e)
            self.conn.rollback()

    def db1_import_db2(self,sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print("------ 数据库导入成功 ------")
            return True
        except Exception as e:
            print(e)
            self.conn.rollback()

    def delete(self, sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print("------ 删除用户成功 ------")
            return True
        except Exception as e:
            print(e)
            self.conn.rollback()

    def insert(self, sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print("----- 插入用户成功 -----")
            return True
        except Exception as e:
            print(e)
            self.conn.rollback()

    def query(self, sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            res_list = self.cur.fetchall()
            for i in res_list:
                print(str(i))
            print("----- 查询个人用户信息成功 -----")
        except Exception as e:
            print(e)
            self.conn.rollback()








